package com.pjh.S3P_1;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;

public class Game extends SurfaceView implements Callback
{
	private Bitmap backImageB;
	private Bitmap mImgBack1;
	private Bitmap mImgBack2;
	private Bitmap mImgBack3;
	
	private Drawable backImage;
	private Drawable Flower1;
	private Drawable Flower2;
	Canvas mCanvas = null;
	Context context ;
	private boolean threadIsAlive = true;

	private ImgThread thread;

	/**
	 * @param context
	 * @param attrs
	 */
	public Game(Context context, AttributeSet attrs)
	{

		super(context, attrs);
		this.context = context;
		Log	.d("haneulho","state : Game(Context context, AttributeSet attrs)");
		SurfaceHolder mHolder = getHolder();
		mHolder.addCallback(this);
		thread = new ImgThread(mHolder, context);

		Resources res = context.getResources();
		// backImage =
		// context.getResources().getDrawable(R.drawable.background);
		// Flower1 =
		// context.getResources().getDrawable(R.drawable.white);
		// Flower2 =
		// context.getResources().getDrawable(R.drawable.yellow);
		backImageB = BitmapFactory.decodeResource(res,
				R.drawable.background);
		mImgBack1 = BitmapFactory.decodeResource(res, R.drawable.white);
		mImgBack2 = BitmapFactory
				.decodeResource(res, R.drawable.yellow);
		Log
				.d("haneulho",
						"state : Game(Context context, AttributeSet attrs) end");

	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void surfaceCreated(SurfaceHolder holder)
	{
		// TODO Auto-generated method stub
		thread.start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder)
	{

		threadIsAlive = false;
		Log.d("haneulho","Destroyed");
		
	}

	class ImgThread extends Thread
	{
		private Bitmap mImgBack;
		private SurfaceHolder mSurfaceHolder;

		public ImgThread(SurfaceHolder surfaceHolder, Context context)
		{
			mSurfaceHolder = surfaceHolder;
			Resources res = context.getResources();
			mImgBack = BitmapFactory.decodeResource(res,	R.drawable.background);

		}
		
		public void doDraw(Canvas canvas)
		{
			canvas.drawBitmap(mImgBack, 0,	0, null);
			canvas.drawBitmap(mImgBack1, 0,	0, null);
			canvas.drawBitmap(mImgBack2, 0,	0, null);
		}
		
		@Override
		public void run()
		{
			while (threadIsAlive)
			{
				Canvas canvas = null;
				try
				{
					canvas = mSurfaceHolder.lockCanvas(null);
					synchronized (mSurfaceHolder)
					{
						doDraw(canvas);
						sleep(100);
					}
				} catch (InterruptedException e)
				{
					e.printStackTrace();
				} finally
				{
					if (canvas != null)
					{
						mSurfaceHolder.unlockCanvasAndPost(canvas);
					}
				}
			}
		}

	}

}
